package org.hzero.interfaces.common.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.hzero.core.base.BaseConstants;
import org.hzero.interfaces.common.constants.KnifeInterfaceCodeEnum;
import org.hzero.interfaces.common.constants.KnifeInterfaceLogDirection;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 接口日志实体
 *
 * @author: tianhao.luo@hand-china.com 2021/6/30  下午12:47
 */
@Table(name = "HITF_INTERFACE_LOG")
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class KnifeInterfaceLogRecord {

    @Id
    @GeneratedValue
    private Long id;

    /**
     * 日志主键(uuid)
     */
    private String invokeKey;
    /**
     * 访问的接口地址
     */
    private String interfaceUrl;

    /**
     * 接口请求方式
     */
    private String interfaceRequestMethod;
    /**
     * 接口触发请求开始时间
     */
    @JsonFormat(pattern = BaseConstants.Pattern.DATETIME)
    @DateTimeFormat(pattern = BaseConstants.Pattern.DATETIME)
    private Date interfaceRequestTime;
    /**
     * 接口收到响应的时间
     */
    @JsonFormat(pattern = BaseConstants.Pattern.DATETIME)
    @DateTimeFormat(pattern = BaseConstants.Pattern.DATETIME)
    private Date interfaceResponseTime;
    /**
     * 接口响应code,如200
     */
    private String interfaceResponseCode;
    /**
     * 接口响应状态,如success
     */
    private String interfaceResponseStatus;
    /**
     * 触发调用接口的方法,如exchange
     */
    private String requestMethod;
    /**
     * 调用请求的时间
     */
    private Long requestTime;
    /**
     * 发起的ip
     */
    private String ip;
    /**
     * 请求头数据
     */
    private String referer;
    /**
     * 请求头中的数据
     */
    private String userAgent;
    /**
     * 请求头参数(超长被截断)
     */
    private String interfaceReqHeaderParam;
    /**
     * 请求url参数(超长不截断)
     */
    private String interfaceReqUrlParam;
    /**
     * 请求url文件id
     */
    private String interfaceReqUrlParamFileId;
    /**
     * 请求体参数(超长不截断),存放在文件中
     */
    private String interfaceReqBodyParam;
    /**
     * 请求体文件id
     */
    private String interfaceReqBodyParamFileId;
    /**
     * 请求响应体(超长不截断),存放在文件中
     */
    private String interfaceRespContent;
    /**
     * 请求响应体文件id
     */
    private String interfaceRespContentFileId;

    private String attribute1;
    private String attribute3;
    private String attribute2;


    /**
     * 该构造方法不支持接口编码及描述的记录，已经过时了
     *
     * @param interfaceRequestTime       请求发起时间
     * @param knifeInterfaceLogDirection 请求方向
     */
    @Deprecated
    public KnifeInterfaceLogRecord(@NotNull Date interfaceRequestTime, @NotNull KnifeInterfaceLogDirection knifeInterfaceLogDirection) {
        Assert.notNull(interfaceRequestTime, "调用接口必须有开始时间");
        this.interfaceRequestTime = interfaceRequestTime;
        // 接口调用方向向外请求则为out向内请求则为in
        this.setAttribute1(knifeInterfaceLogDirection.name());
    }

    /**
     * 构造接口日志
     *
     * @param interfaceRequestTime       请求发起时间
     * @param knifeInterfaceLogDirection 请求方向
     * @param knifeInterfaceCodeEnum     接口编码和接口描述
     */
    public KnifeInterfaceLogRecord(@NotNull Date interfaceRequestTime,
                                   @NotNull KnifeInterfaceLogDirection knifeInterfaceLogDirection,
                                   KnifeInterfaceCodeEnum knifeInterfaceCodeEnum) {
        Assert.notNull(interfaceRequestTime, "调用接口必须有开始时间");
        this.interfaceRequestTime = interfaceRequestTime;
        // 接口调用方向向外请求则为out向内请求则为in
        this.setAttribute1(knifeInterfaceLogDirection.name());
        // 添加描述
        this.setAttribute2(knifeInterfaceCodeEnum.getInterfaceCode());
        this.setAttribute3(knifeInterfaceCodeEnum.getDescription());
    }

    public KnifeInterfaceLogRecord() {
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAttribute1() {
        return attribute1;
    }

    public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
    }

    public String getAttribute3() {
        return attribute3;
    }

    public void setAttribute3(String attribute3) {
        this.attribute3 = attribute3;
    }

    public String getAttribute2() {
        return attribute2;
    }

    public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
    }

    public String getInterfaceReqUrlParam() {
        return interfaceReqUrlParam;
    }

    public void setInterfaceReqUrlParam(String interfaceReqUrlParam) {
        this.interfaceReqUrlParam = interfaceReqUrlParam;
    }

    public String getInterfaceRespContentFileId() {
        return interfaceRespContentFileId;
    }

    public void setInterfaceRespContentFileId(String interfaceRespContentFileId) {
        this.interfaceRespContentFileId = interfaceRespContentFileId;
    }

    public String getInterfaceRespContent() {
        return interfaceRespContent;
    }

    public void setInterfaceRespContent(String interfaceRespContent) {
        this.interfaceRespContent = interfaceRespContent;
    }

    public String getInvokeKey() {
        return invokeKey;
    }

    public void setInvokeKey(String invokeKey) {
        this.invokeKey = invokeKey;
    }

    public String getInterfaceUrl() {
        return interfaceUrl;
    }

    public void setInterfaceUrl(String interfaceUrl) {
        this.interfaceUrl = interfaceUrl;
    }


    public String getInterfaceRequestMethod() {
        return interfaceRequestMethod;
    }

    public void setInterfaceRequestMethod(String interfaceRequestMethod) {
        this.interfaceRequestMethod = interfaceRequestMethod;
    }

    public Date getInterfaceRequestTime() {
        return interfaceRequestTime;
    }

    public void setInterfaceRequestTime(Date interfaceRequestTime) {
        this.interfaceRequestTime = interfaceRequestTime;
    }

    public Date getInterfaceResponseTime() {
        return interfaceResponseTime;
    }

    public void setInterfaceResponseTime(Date interfaceResponseTime) {
        this.interfaceResponseTime = interfaceResponseTime;
    }

    public String getInterfaceResponseCode() {
        return interfaceResponseCode;
    }

    public void setInterfaceResponseCode(String interfaceResponseCode) {
        this.interfaceResponseCode = interfaceResponseCode;
    }

    public Long getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(Long requestTime) {
        this.requestTime = requestTime;
    }

    public String getInterfaceResponseStatus() {
        return interfaceResponseStatus;
    }

    public void setInterfaceResponseStatus(String interfaceResponseStatus) {
        this.interfaceResponseStatus = interfaceResponseStatus;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getInterfaceReqHeaderParam() {
        return interfaceReqHeaderParam;
    }

    public void setInterfaceReqHeaderParam(String interfaceReqHeaderParam) {
        this.interfaceReqHeaderParam = interfaceReqHeaderParam;
    }

    public String getInterfaceReqBodyParam() {
        return interfaceReqBodyParam;
    }

    public void setInterfaceReqBodyParam(String interfaceReqBodyParam) {
        this.interfaceReqBodyParam = interfaceReqBodyParam;
    }

    public String getInterfaceReqBodyParamFileId() {
        return interfaceReqBodyParamFileId;
    }

    public void setInterfaceReqBodyParamFileId(String interfaceReqBodyParamFileId) {
        this.interfaceReqBodyParamFileId = interfaceReqBodyParamFileId;
    }

    public String getInterfaceReqUrlParamFileId() {
        return interfaceReqUrlParamFileId;
    }

    public void setInterfaceReqUrlParamFileId(String interfaceReqUrlParamFileId) {
        this.interfaceReqUrlParamFileId = interfaceReqUrlParamFileId;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("HitfInterfaceLog{");
        sb.append("invokeKey='").append(invokeKey).append('\'');
        sb.append(", interfaceUrl='").append(interfaceUrl).append('\'');
        sb.append(", interfaceRequestMethod='").append(interfaceRequestMethod).append('\'');
        sb.append(", interfaceRequestTime=").append(interfaceRequestTime);
        sb.append(", interfaceResponseTime=").append(interfaceResponseTime);
        sb.append(", interfaceResponseCode='").append(interfaceResponseCode).append('\'');
        sb.append(", interfaceResponseStatus='").append(interfaceResponseStatus).append('\'');
        sb.append(", requestMethod='").append(requestMethod).append('\'');
        sb.append(", requestTime=").append(requestTime);
        sb.append(", ip='").append(ip).append('\'');
        sb.append(", referer='").append(referer).append('\'');
        sb.append(", userAgent='").append(userAgent).append('\'');
        sb.append(", interfaceReqHeaderParam='").append(interfaceReqHeaderParam).append('\'');
        sb.append(", interfaceReqUrlParam='").append(interfaceReqUrlParam).append('\'');
        sb.append(", interfaceReqUrlParamFileId='").append(interfaceReqUrlParamFileId).append('\'');
        sb.append(", interfaceReqBodyParam='").append(interfaceReqBodyParam).append('\'');
        sb.append(", interfaceReqBodyParamFileId='").append(interfaceReqBodyParamFileId).append('\'');
        sb.append(", interfaceRespContent='").append(interfaceRespContent).append('\'');
        sb.append(", interfaceRespContentFileId='").append(interfaceRespContentFileId).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
